using System;
using System.Collections;
using System.Collections.Generic;
using GameFramework;
using UnityEngine;

namespace LS.Tools
{
    public class FrameInvoker: IReference
    {
        private List<Action> m_InvokerList;
        private List<Action> m_TempInvokerList;

        public int Count
        {
            get
            {
                return m_InvokerList == null ? 0 : m_InvokerList.Count;
            }
        }
        public FrameInvoker()
        {
            m_InvokerList = new List<Action>();
            m_TempInvokerList = new List<Action>();
        }

        public void Add(Action act)
        {
            m_InvokerList.Add(act);
        }

        public void Remove(Action act)
        {
            m_InvokerList.Remove(act);
        }

        public void UpdateInvoke()
        {
            if (m_InvokerList.Count == 0)
            {
                return;
            }

            m_TempInvokerList.AddRange(m_InvokerList);
            m_InvokerList.Clear();
            for (int i = 0; i < m_TempInvokerList.Count; i++)
            {
                m_TempInvokerList[i]?.Invoke();
            }
            m_TempInvokerList.Clear();
        }

        public void Clear()
        {
            m_InvokerList.Clear();
            m_TempInvokerList.Clear();
        }
    }
}
